package org.zcz.svnpackageutil;

import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

public class Demo {
    public static void main(String[] args) {
        DAVRepositoryFactory.setup();
        SVNRepositoryFactoryImpl.setup();
        FSRepositoryFactory.setup();

        String url = "https://svn.bytter.com/svn/bytter2017/南玻集团一期/实现/代码/v106BS/";
        String username = "zhangcz";
        String password = "Zcz201903";

        long startRevision = 0;
        long endRevision = 10;

        SVNRepository repository = null;
        try {
            repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url));

            ISVNAuthenticationManager authenticationManager = SVNWCUtil.createDefaultAuthenticationManager(username,password);
            repository.setAuthenticationManager(authenticationManager);

            Collection logs = null;
            logs = repository.log(new String[]{""},null,startRevision,endRevision,true,true);

            for (Iterator entries = logs.iterator();entries.hasNext();){
                SVNLogEntry logEntry = (SVNLogEntry)entries.next();
                System.out.println( "---------------------------------------------" );
                System.out.println ("revision: " + logEntry.getRevision( ) );
                System.out.println( "author: " + logEntry.getAuthor( ) );
                System.out.println( "date: " + logEntry.getDate( ) );
                System.out.println( "log message: " + logEntry.getMessage( ) );
                if ( logEntry.getChangedPaths( ).size( ) > 0 ) {
                    System.out.println( );
                    System.out.println( "changed paths:" );
                    Set changedPathsSet = logEntry.getChangedPaths( ).keySet( );

                    for ( Iterator changedPaths = changedPathsSet.iterator( ); changedPaths.hasNext( ); ) {
                        SVNLogEntryPath entryPath = (SVNLogEntryPath) logEntry.getChangedPaths( ).get( changedPaths.next( ) );
                        System.out.println( " "
                                + entryPath.getType( )
                                + " "
                                + entryPath.getPath( )
                                + ( ( entryPath.getCopyPath( ) != null ) ? " (from "
                                + entryPath.getCopyPath( ) + " revision "
                                + entryPath.getCopyRevision( ) + ")" : "" ) );
                        }
                    }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
